"use strict"
//获取服务器列表
module.exports = async function () {

    let _access=this.get('access','string');
    let developModel = this.model.redis("develop",this.branch);
    let _isDev=await developModel.get(_access);
    let status=[1,2];
    let sidMax=900;
    if(_isDev){
         //开发者不做任何过滤,且包含所有的区服
        status.push(0);
        sidMax=10000;
    }

    let keys = ["remote","name","time","online","status"];
    let query = {};
    let option = {"multi":true,"dataType":"json"};
    let server = this.model.branch("server",this.branch);
    option["fields"] = server.fields(keys);
    status = status || [1,2];
    if(Array.isArray(status)){
        query['status'] = {"$in":status};
    }
    else{
        query['status'] = status;
    }
    query['_id']={"$lt":sidMax};
    let _servers=await server.find(query,option);
    // console.log(`isDev:${_isDev}`);
    // console.log(_servers);
    return _servers;
    // if(guid){
    //      let innerAccModel = this.model.branch("innerAcc",this.branch);
    //      let _guid_ret=await innerAccModel.find({'guid':guid});
    //      console.log(_guid_ret);
    //      return _servers;
    // }
    // else{
    //     return _servers;
    // }
};




// module.exports = function () {
//     let version = this.get("version");
//     return Promise.resolve().then(()=>{
//         let ip = this.req.ip;
//         let iptable = this.model.branch("iptable",this.branch);
//         return iptable.verify(ip)
//     }).then(ret=>{
//         console.log(this.opensdk);
//         //在白名单内的全部放行
//         if(ret){
//             return [0,1,2,9];
//         }
//         else if(version){
//             return verify_version.call(this,version,this.opensdk['version']);
//         }
//     }).then(status=>{
//         let keys = ["remote","name","time","online","status","ipFilter"];
//         let query = {};
//         let option = {"multi":true,"dataType":"json"};
//         let server = this.model.branch("server",this.branch);
//         option["fields"] = server.fields(keys);

//         status = status || [1,2];
//         if(Array.isArray(status)){
//             query['status'] = {"$in":status};
//         }
//         else{
//             query['status'] = status;
//         }

//         return server.find(query,option);
//     })
// };


function verify_version(ver1,ver2){
    if(!ver1 || !ver2){
        return false;
    }
    let arr1 = String(ver1).split('.');
    let arr2 = String(ver2).split('.');
    for(let i=0;i<3;i++){
        if(parseInt(arr1[i]||0) > parseInt(arr2[i]|0)){
            return 9;
        }
    }
    return false;
}